package cn.sxt.bus.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.sxt.bus.domain.Goods;
import cn.sxt.bus.domain.Outport;
import cn.sxt.bus.mapper.GoodsMapper;
import cn.sxt.bus.mapper.OutportMapper;
import cn.sxt.bus.service.OutportService;
import cn.sxt.bus.vo.GoodsVo;
import cn.sxt.bus.vo.OutportVo;
import cn.sxt.sys.utils.DataGridView;

@Service
public class OutportServiceImpl implements OutportService {

	@Autowired
	private OutportMapper outportMapper;

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public DataGridView queryAllOutport(OutportVo outportVo) {
		Page<Object> page = PageHelper.startPage(outportVo.getPage(), outportVo.getLimit());
		List<OutportVo> list = outportMapper.queryAllOutport(outportVo);
		return new DataGridView(page.getTotal(), list);
	}

	@Override
	public List<OutportVo> queryAllOutportForList(OutportVo outportVo) {
		// TODO Auto-generated method stub
		return outportMapper.queryAllOutport(outportVo);
	}

	@Override
	public int deleteOutportById(OutportVo outportVo) {
		// 得到这个商品的id
		Outport outport = outportMapper.selectByPrimaryKey(outportVo.getId());
		Integer goodsid = outport.getGoodsid();
		// 查这个商品之前的库存
		Goods goods = goodsMapper.selectByPrimaryKey(goodsid);
		Integer oldnum = goods.getNumber();
		// 删除前的退货数量
		Integer beginnum = outport.getNumber();
		// 删除
		int i = outportMapper.deleteByPrimaryKey(outportVo.getId());

		// 再把 退货数量 +到 商品 库存里
		goods.setNumber(oldnum + beginnum);
		int j = goodsMapper.updateByPrimaryKeySelective(goods);
		return 1;
	}

	@Override
	public int addOutport(OutportVo outportVo) {
		// 先退货
		int i = outportMapper.insert(outportVo);

		// -------修改商品的库存----
		// 得到这个商品的id
		Integer goodsid = outportVo.getGoodsid();
		// 查这个商品之前的库存
		Goods goods = goodsMapper.selectByPrimaryKey(goodsid);
		Integer oldnum = goods.getNumber();
		// 得到退货的 数量
		Integer number = outportVo.getNumber();
		// 再把 减去退货数量 ，在 商品 库存里
		goods.setNumber(oldnum - number);
		int j = goodsMapper.updateByPrimaryKeySelective(goods);
		return 1;
	}

	@Override
	public Outport queryOutportById(Integer id) {
		// TODO Auto-generated method stub
		return outportMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateOutport(OutportVo outportVo) {
		// 得到这个商品的id
		Outport outport = outportMapper.selectByPrimaryKey(outportVo.getId());
		Integer goodsid = outport.getGoodsid();
		// 查这个商品之前的库存
		Goods goods = goodsMapper.selectByPrimaryKey(goodsid);
		Integer oldnum = goods.getNumber();
		// 修改前的退货数量
		Integer beginnum = outport.getNumber();
		// 修改退货
		int i = outportMapper.updateByPrimaryKeySelective(outportVo);

		// 得到 修改后 退货的 数量
		Integer number = outportVo.getNumber();
		// 再把 修改后退货数量 +到 商品 库存里
		// 库存 + 修改前的退货量 - 修改后的退货量
		
		goods.setNumber(oldnum + beginnum - number);
		int j = goodsMapper.updateByPrimaryKeySelective(goods);
		return 1;
	}

	@Override
	public OutportVo queryOutportVoById(Integer id) {
		// TODO Auto-generated method stub
		return outportMapper.queryOutportVoById(id);
	}

}
